package com.example.laixunlong.control;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.laixunlong.R;

import java.util.Calendar;

public class AlertDialog_duihua extends AppCompatActivity
{
    Button but1,but2,but3,but4,but5,but6;
    DatePicker date;
    TimePicker time;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alertdialog_duihua);

        /*设置点击弹出日历事件*/
        but1=findViewById(R.id.date_button_1);
        date=findViewById(R.id.date_picker_1);
        but5=findViewById(R.id.date_button_3);
        but1.setOnClickListener(new View.OnClickListener() {//弹出日历控件
            @Override
            public void onClick(View v) {
                date.setVisibility(View.VISIBLE);
            }
        });
        but5.setOnClickListener(new View.OnClickListener() {//弹出日历对话框
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                DatePickerDialog timewin=new DatePickerDialog(AlertDialog_duihua.this,
                        //android.R.style.Widget_Holo,/*设置主题*/
                        new DatePickerDialog.OnDateSetListener() {/*设置确定按钮监听事件*/
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                Toast.makeText( AlertDialog_duihua.this,"获取到时间:"+year+"年"+(month+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
                                Log.e("获取到时间",year+"年"+month+"月"+dayOfMonth+"日");
                            }
                        },
                        calendar.get(Calendar.YEAR),/*设置默认年*/
                        calendar.get(Calendar.MONTH),/*设置默认月*/
                        calendar.get(Calendar.DAY_OF_MONTH)/*设置默认日*/);
                timewin.show();
            }
        });
        /*将时间数据上传到控制台*/
        but2=findViewById(R.id.date_button_2);
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nian=date.getYear()+"";
                String yue=(date.getMonth()+1)+"";
                String ri=date.getDayOfMonth()+"";
                String time=nian+"获取到时间:"+nian+"年"+yue+"月"+ri+"日";
                Toast.makeText(AlertDialog_duihua.this,time,Toast.LENGTH_SHORT).show();
                Log.e("获取到时间",nian+"年"+yue+"月"+ri+"日");
            }
        });
        /*打开准确时间对话框*/
        time=findViewById(R.id.time_picker_1);
        time.setIs24HourView(true);//设置滚动为24小时制
        but3=findViewById(R.id.time_button_1);
        but6=findViewById(R.id.time_button_3);
        but3.setOnClickListener(new View.OnClickListener() {//弹出时间控件
            @Override
            public void onClick(View v) {
                time.setVisibility(View.VISIBLE);
            }
        });
        but6.setOnClickListener(new View.OnClickListener() {//弹出时间对话框
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                TimePickerDialog timewin=new TimePickerDialog(AlertDialog_duihua.this,
                    android.R.style.Theme_Holo_Light_Dialog,/*设置主题,滚动式*/
                    new TimePickerDialog.OnTimeSetListener() {/*设置监听事件*/
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            String t="获取到时间:"+hourOfDay+"时"+minute+"分";
                            Toast.makeText( AlertDialog_duihua.this,t,Toast.LENGTH_SHORT).show();
                            Log.e("获取到时间",t);
                        }
                    },
                    calendar.get(Calendar.HOUR_OF_DAY),/*设置默认时*/
                    calendar.get(Calendar.MINUTE),/*设置默认分*/
                    true);/*开启24小时制*/
                timewin.show();
            }
        });
        /*将时间上传到控制台*/
        but4=findViewById(R.id.time_button_2);
        but4.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                String shi=time.getHour()+"";
                String fen=time.getMinute()+"";
                String t="获取到时间:"+shi+"时"+fen+"分";
                Toast.makeText( AlertDialog_duihua.this,t,Toast.LENGTH_SHORT).show();
                Log.e("获取到时间",t);
            }
        });
    }
    public void tanchu(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        /*总能看到View参数,那么View有什么用,怎么获取,请参详如下代码.
        *   view表示布局对象,一般使用静态方法getLayoutInflater().inflate( , )获取.
        *   此对象如果使用在对话框的setView中,则只有前50dp是生效的.*/
        View view=getLayoutInflater().inflate(R.layout.dialog_tanchuang,null);

        builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("这是对话框")
                .setMessage("这是对话框内容.")
                .setView(view)
                /*以下三个按钮的样式设定是根据系统版本内置的,开发人员无法对其进行编辑*/
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("对话按钮", "确认按钮被点击");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("对话按钮", "取消按钮被点击");
                    }
                })
                .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("对话按钮", "中间按钮被点击");
                    }
                })
                .create()
                .show();
    }
}
